
$(function() {
	
	$("input[id^='mold_s']:visible").autocomplete(Test.mold);
	
	$("#applicant_s").autocomplete(Test.emp);
	
	$("#checker_s").autocomplete(Test.emp);
	
	Tip.emp('负责人');
});

var Testing = {
	ss: 2,

	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.mold = form.mold.value;
				body.piece = form.piece.value;
				body.work = form.work.value;
				body.applicant = form.applicant.value;

				body.checker = form.checker.value;
				body.date = form.date.value;
				body.status = form.status.value;
				
				body.amount = form.amount.value;
				body.material = form.material.value;

				body.content = form.content.value;
				body.exception = form.exception.value;		

				$(this).dialog("close");
				
				var url = 'mold.php?do=Testing.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	

	popup: function (e, p2, id, status) {	
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);
		
		var c1 = 'mold.php?do=Testing.edit&id=' + id;
		var c2 = 'mold.php?do=Testing.view&id=' + id;
		var c3 = 'mold.php?do=Testing.copy&id=' + id;
		
		var c4 = 'mold.php?do=Testing.solution&id=' + id;

		var a1 = ['编辑信息', 'url', 'Testing.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];	
		var a3 = ['复制信息', 'url', 'Testing.dialog_copy_', '650x400'];
		var a4 = ['解决方法', 'url', 'Testing.dialog_solution_', '650x400'];

		if (status == 1) {
			var s = c1 + ", " + c2 + ", " + c3 + ", " + c4;

			return popup(e, [a1, a2, a3, a4], s);
		}
		
		var s = c1 + ", " + c2 + ", " + c3;

		popup(e, [a1, a2, a3], s);
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=Testing.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.mold = form.mold.value;
						body.piece = form.piece.value;
						body.work = form.work.value;
						body.applicant = form.applicant.value;

						body.checker = form.checker.value;
						body.date = form.date.value;
						body.status = form.status.value;
						
						body.amount = form.amount.value;
						body.material = form.material.value;

						body.content = form.content.value;
						body.exception = form.exception.value;
						body.solution = form.solution.value;

						$(this).dialog("close");

						var url = 'mold.php?do=Testing.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.mold = form.mold.value;
				body.piece = form.piece.value;
				body.work = form.work.value;
				body.applicant = form.applicant.value;

				body.checker = form.checker.value;
				body.date = form.date.value;
				body.status = form.status.value;
				
				body.amount = form.amount.value;
				body.material = form.material.value;

				body.content = form.content.value;
				body.exception = form.exception.value;		

				$(this).dialog("close");
				
				var url = 'mold.php?do=Testing.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_solution_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=Testing.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;

						body.solution = form.solution.value;
						body.content = form.content.value;					

						$(this).dialog("close");

						var url = 'mold.php?do=Testing.solutionPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	}
}
